package com.vgooo.client;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TabHost;
import android.widget.TextView;

import com.nostra13.universalimageloader.cache.memory.MemoryCacheAware;
import com.nostra13.universalimageloader.cache.memory.impl.LRULimitedMemoryCache;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.umeng.analytics.MobclickAgent;
import com.umeng.fb.NotificationType;
import com.umeng.fb.UMFeedbackService;
import com.umeng.update.UmengDownloadListener;
import com.umeng.update.UmengUpdateAgent;
import com.vgooo.client.index.IndexSearchActivity;
import com.vgooo.client.mobile.MobileAlbumDetail;
import com.vgooo.client.mobile.MobileDetailActivity;
import com.vgooo.client.mobile.MobileMainFragmentActivity;
import com.vgooo.client.myvg.LoginActivity;
import com.vgooo.client.myvg.MyLoginActivity;
import com.vgooo.client.myvg.MyVGActivity;
import com.vgooo.client.myvg.PhotoActivity;
import com.vgooo.client.myvg.RegisterActivity;
import com.vgooo.client.tool.ToolMainActivity;
import com.vgooo.client.tool.branch.BranchMapActivity;
import com.vgooo.client.tool.branch.BrandsMainActivity;
import com.vgooo.client.tool.tester.TestActivity;
import com.vgooo.client.vchannel.NewsDetailActivity;
import com.vgooo.client.vchannel.PingActivity;
import com.vgooo.client.vchannel.VChannelFragmentActivity;
import com.vgooo.client.vchannel.VideoDetailActivity;
import com.vgooo.client.vchannel.album.AlbumDetailActivity;
import com.vgooo.util.BitmapUtils;
import com.vgooo.util.BitmapUtils.PictureLoadingThreadPool;
import com.vgooo.util.DebugTools;
import com.vgooo.util.HttpUtils;

/**
 * TabHost主界面
 * @author alee
 * 
 */
public class Main extends TabActivity {
	private final static String className = "MainActivity";

	public static Activity activity;

	public static TabHost tabHost;

	public static Intent homeIntent, moblieIntent, moblieAlbumIntent,
			vchannelIntent, myvgIntent, toolIntent;

	public static Intent searchIntent, advDetailIntent, moblieDetailIntent,
			loginIntent, myLoginIntent, photoIntent, branchsMain,
			branchMapIntent, test_mainIntent, album_detailIntent,
			vedio_detailIntent, news_detailIntent, imageZoomIntent,
			registerIntent, pingIntent;

	// 记录当前显示的activity
	public static int currentTabId = 0xff;

	public static Animation left_in, left_out;
	public static Animation right_in, right_out;

	public static View back, logo, tool, search;

	public static TextView title;

	// 顶部标题头和底部导航栏
	public static View tileLayout, navLayout;

	public static View moblieNav, vchannelNav, vGNav;

	public static boolean isFist = true;

	// public static PictureLoadingThreadPool picLading ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		umengTool();

		// ImageLoader Setting
		int memoryCacheSize = (int) (Runtime.getRuntime().maxMemory() / 8);

		MemoryCacheAware<String, Bitmap> memoryCache;//这个类有什么用
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
			memoryCache = new LruMemoryCache(memoryCacheSize);
		} else {
			memoryCache = new LRULimitedMemoryCache(memoryCacheSize);
		}
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				getApplicationContext()).memoryCache(memoryCache)
				.denyCacheImageMultipleSizesInMemory()
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.offOutOfMemoryHandling()
				// .enableLogging()
				.defaultDisplayImageOptions(BitmapUtils.options).build();
		BitmapUtils.imageLoader.init(config);
		// 设置缓存路径
		VGApplication appliction = (VGApplication) getApplication();
		appliction.setCaesPath(getCacheDir().toString());
		BitmapUtils.albumLoadPool = new PictureLoadingThreadPool(this, 5);

		setContentView(R.layout.main);

		currentTabId = 0xff;
		activity = this;

		// picLading = new PictureLoadingThreadPool(this) ;
		isFist = true;
		initView();
		prepareAnim();
		if (isFist) {
			prepareIntent();
			setupIntent();
			isFist = false;
		}

		DebugTools.log(className, "---onCreate---");

	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		DebugTools.log(className, "---onStart---");
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		DebugTools.log(className, "---onRestart---");
	}

	@Override
	protected void onResume() {
		if (isFist) {
			prepareIntent();
			setupIntent();
			isFist = false;
		} else {
			// if(ViewLayout.viwStack==null||ViewLayout.viwStack.isEmpty()){
			// if(ViewLayout.viwStack==null){
			// ViewLayout.viwStack =new Stack<ViewLayout>();
			// }
			// if(ViewLayout.viwStack.isEmpty()||ViewLayout.viwStack.size()<1){
			// ViewLayout.setView(true, "手机中心", ViewLayout.TAB_TAG_MOBILE, true,
			// null);
			// }
			// }
		}

		super.onResume();
		DebugTools.log(className, "---onResume---");
		// 友盟统计功能，打开页面
		MobclickAgent.onResume(this);
		HttpUtils.needColseConn = false;
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		DebugTools.log(className, "---onPause---");
		// 友盟统计功能，页面关闭
		MobclickAgent.onPause(this);
		HttpUtils.needColseConn = false;
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		DebugTools.log(className, "---onStop---");
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		DebugTools.log(className, "---onDestroy---");
		// picLading.exit() ;
		// ViewLayout.viwStack.clear() ;
		isFist = true;
		activity = null;
		tabHost.removeAllViews();
		tabHost.destroyDrawingCache();
		System.gc();
	}

	private void prepareAnim() {
		left_in = AnimationUtils.loadAnimation(this, R.anim.left_in);
		left_out = AnimationUtils.loadAnimation(this, R.anim.left_out);
		right_in = AnimationUtils.loadAnimation(this, R.anim.right_in);
		right_out = AnimationUtils.loadAnimation(this, R.anim.right_out);
	}

	private void initView() {
		back = findViewById(R.id.home_select_1back);
		logo = findViewById(R.id.index_head_logo);
		search = findViewById(R.id.index_head_search);
		tool = findViewById(R.id.home_select_5tool);

		title = (TextView) findViewById(R.id.index_head_title);
		// title2 = (TextView) findViewById(R.id.index_head_title2);

		tileLayout = findViewById(R.id.main_tileLayout);
		navLayout = findViewById(R.id.main_navLayout);

		moblieNav = findViewById(R.id.home_select_2mobile);
		vchannelNav = findViewById(R.id.home_select_3vchannel);
		vGNav = findViewById(R.id.home_select_4myvg);
	}

	private void prepareIntent() {
		// homeIntent = new Intent(this, IndexActivity.class);
		moblieIntent = new Intent(this, MobileMainFragmentActivity.class);
		vchannelIntent = new Intent(this, VChannelFragmentActivity.class);
		myvgIntent = new Intent(this, MyVGActivity.class);
		toolIntent = new Intent(this, ToolMainActivity.class);
		searchIntent = new Intent(this, IndexSearchActivity.class);

		// advDetailIntent = new Intent(this, AdvDetailActivity.class);
		// moblieDetailIntent = new Intent(this, MobileDetailActivity.class);
		moblieDetailIntent = new Intent(this, MobileDetailActivity.class);
		moblieAlbumIntent = new Intent(this, MobileAlbumDetail.class);
		loginIntent = new Intent(this, LoginActivity.class);
		myLoginIntent = new Intent(this, MyLoginActivity.class);
		photoIntent = new Intent(this, PhotoActivity.class);
		branchsMain = new Intent(this, BrandsMainActivity.class);
		branchMapIntent = new Intent(this, BranchMapActivity.class);
		test_mainIntent = new Intent(this, TestActivity.class);
		album_detailIntent = new Intent(this, AlbumDetailActivity.class);
		vedio_detailIntent = new Intent(this, VideoDetailActivity.class);
		news_detailIntent = new Intent(this, NewsDetailActivity.class);
		imageZoomIntent = new Intent(this, ImageZoomActivity.class);
		registerIntent = new Intent(this, RegisterActivity.class);
		pingIntent = new Intent(this, PingActivity.class);

	}

	private void setupIntent() {
		tabHost = getTabHost();
		// tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_HOME,
		// homeIntent));//暂时去除首页
		tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_MOBILE, moblieIntent));
		tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_VCHANNEL, vchannelIntent));
		tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_MYVG, myvgIntent));
		tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_LOGIN, loginIntent));
		tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_MYLOGIN, myLoginIntent));
		tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_TOOL, toolIntent));

		tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_SEARCH, searchIntent));
		tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_ADV_DETAIL,
				advDetailIntent));
		tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_MOBILE_DETAIL,
				moblieDetailIntent));
		tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_MOBILE_ALBUM,
				moblieAlbumIntent));
		tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_PHOTO, photoIntent));
		tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_BRANCHS_MAIN, branchsMain));
		tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_BRANCHS_MAP,
				branchMapIntent));
		tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_TEST_MAIN,
				test_mainIntent));
		tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_ALBUM_DETAIL,
				album_detailIntent));
		tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_VEDEIO_DETAIL,
				vedio_detailIntent));
		tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_NEWS_DETAIL,
				news_detailIntent));
		tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_IMAGEZOOM,
				imageZoomIntent));
		tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_REGISTER, registerIntent));
		tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_PING, pingIntent));

		// tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_TEST_AUDIO,
		// testAudioIntent));
		// tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_TEST_BUTTON,
		// testButtonIntent));
		// tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_TEST_CAMERA,
		// testCameraIntent));
		// tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_TEST_COMMUN,
		// testCommunIntent));
		// tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_TEST_DISPLAY,
		// testDisplayIntent));
		// tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_TEST_HST,
		// testHstIntent));
		// tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_TEST_LED,
		// testLedIntent));
		// tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_TEST_LIGHT,
		// testLightIntent));
		// tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_TEST_SHAKE,
		// testShakeIntent));
		// tabHost.addTab(buildTaSpec(ViewLayout.TAB_TAG_TEST_TOUCH,
		// testTouchIntent));
	}

	private TabHost.TabSpec buildTaSpec(String tag, final Intent content) {
		return tabHost.newTabSpec(tag).setIndicator(tag).setContent(content);
	}

	public static void setCurrentTabByTag(String tab) {
		tabHost.setCurrentTabByTag(tab);
	}

	public static void setNavBackground(int tabId) {
		switch (tabId) {
		case R.id.home_select_2mobile:
			moblieNav.setBackgroundResource(R.drawable.bottom_bg__fous);
			vchannelNav.setBackgroundResource(R.drawable.bottom_bg_no_fous);
			vGNav.setBackgroundResource(R.drawable.bottom_bg_no_fous);
			break;
		case R.id.home_select_3vchannel:
			moblieNav.setBackgroundResource(R.drawable.bottom_bg_no_fous);
			vchannelNav.setBackgroundResource(R.drawable.bottom_bg__fous);
			vGNav.setBackgroundResource(R.drawable.bottom_bg_no_fous);
			break;
		case R.id.home_select_4myvg:
			moblieNav.setBackgroundResource(R.drawable.bottom_bg_no_fous);
			vchannelNav.setBackgroundResource(R.drawable.bottom_bg_no_fous);
			vGNav.setBackgroundResource(R.drawable.bottom_bg__fous);
			break;

		default:
			moblieNav.setBackgroundResource(R.drawable.bottom_bg_no_fous);
			vchannelNav.setBackgroundResource(R.drawable.bottom_bg_no_fous);
			vGNav.setBackgroundResource(R.drawable.bottom_bg_no_fous);
			break;
		}
	}

	/**
	 * 控件的单击事件
	 * 
	 * @param v
	 *            单击的控件
	 */
	public void doClick(View v) {
		if (currentTabId == v.getId()) {
			return;
		}

		switch (v.getId()) {
		case R.id.home_select_1back:
			ViewLayout.setViewBack();
			break;

		case R.id.home_select_2mobile:
			ViewLayout.setViewPre(true, "手机中心", ViewLayout.TAB_TAG_MOBILE,
					ViewLayout.ID_TAG_MOBILE, this);
			break;
		case R.id.home_select_3vchannel:
			// Toast.makeText(this, "敬请期待..", Toast.LENGTH_LONG).show();

			ViewLayout.setViewPre(true, "V频道", ViewLayout.TAB_TAG_VCHANNEL,
					ViewLayout.ID_TAG_VCHANNEL, this);
			break;
		case R.id.home_select_4myvg:
			// Toast.makeText(this, "我的VG", Toast.LENGTH_SHORT).show();
			log("我的VG");
			// ViewLayout.viwStack.push(new ViewLayout(true, "我的VG",
			// ViewLayout.ID_TAG_MYVG,
			// ViewLayout.TAB_TAG_MYVG,activity));
			// ViewLayout.setViewPre(true, "登录", ViewLayout.TAB_TAG_LOGIN,this);
			// ViewLayout.setViewPre(true, "登录",
			// ViewLayout.TAB_TAG_MYLOGIN,this);
			ViewLayout.setViewPre(true, "我的VG", ViewLayout.TAB_TAG_MYVG,
					ViewLayout.ID_TAG_MYVG, this);

			break;
		case R.id.home_select_5tool:
			ViewLayout.setViewPre(true, "工具", ViewLayout.TAB_TAG_TOOL,
					ViewLayout.ID_TAG_TOOL, this);
			break;

		case R.id.index_head_search:
			ViewLayout.setViewPre(true, "搜索", ViewLayout.TAB_TAG_SEARCH,
					ViewLayout.ID_TAG_SEARCH, this);
			break;

		default:
			DebugTools.log(className, "未定义单击事件。。。");
			break;
		}
	}

	//
	// public static void QQLogin(){
	// QQOauthorizeAction action = new QQOauthorizeAction(activity, null, null);
	// }
	// private QQOauthorizeAction action = null ;
	// @Override
	// protected void onActivityResult(int requestCode, int resultCode, Intent
	// data) {
	// // TODO Auto-generated method stub
	// if(null!=action){
	// action.getTencent().onActivityResult(requestCode, resultCode, data);
	// }
	//
	// }

	private void umengTool() {

		// 友盟用户反馈提示功能
		UMFeedbackService.enableNewReplyNotification(this,
				NotificationType.AlertDialog);
		UMFeedbackService.setGoBackButtonVisible();
		// 友盟自动捕或异常功能
		MobclickAgent.onError(this);
		MobclickAgent.setDebugMode(false);
		// 友盟自动更新功能
		UmengUpdateAgent.update(this);
		// 弹出更新对话框
		UmengUpdateAgent.setUpdateAutoPopup(true);
		UmengUpdateAgent.setOnDownloadListener(new UmengDownloadListener() {

			@Override
			public void OnDownloadEnd(int result) {
				// TODO Auto-generated method stub
				// result更新回调接口 值 1： 代表成功 0：失败
				String str = null;
				switch (result) {
				case 0:
					str = "下载失败。。。";
					break;
				case 1:
					str = "下载成功。。。";
					break;
				default:
					break;
				}
				// Toast.makeText(Main.this, str, Toast.LENGTH_LONG).show();
			}
		});
		// 设置是否只在连接wifi的状态下更新
		UmengUpdateAgent.setUpdateOnlyWifi(false);

	}

	private static void log(String info) {
		Log.i("Main", info);
	}

}
